﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KaroglanLauncher
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class ModInfoWidget : UserControl
    {
        private MainWindow parent;
        private ModObj _modModObj;

        public ModInfoWidget()
        {
            InitializeComponent();
        }

        public ModInfoWidget(ModObj modObj, MainWindow mWindow)
        {
            InitializeComponent();
            parent = mWindow;
            _modModObj = modObj;
            button.Click += SwitchActive;
            Refresh();
        }

        public void Refresh() 
        {
            Type.Content = _modModObj.type;
            DisplayName.Content = _modModObj.displayName;
            RefreshBtnContent();
        }

        public void RefreshBtnContent()
        {
            button.Content = _modModObj.enabled ? "禁用" : "启用";
        }

        private void SwitchActive(object sender, RoutedEventArgs e)
        {
            var newState = !_modModObj.enabled;
            try
            {
                if (newState && _modModObj.requiredMod.Count > 0)
                {
                    foreach (var modId in _modModObj.requiredMod)
                    {
                        if (ModManager.GetData().Keys.Contains(modId))
                        {
                            var reqModInfo = ModManager.GetData()[modId];
                            reqModInfo.SetActiveState(true);
                            if (parent.widgetDict.Keys.Contains(modId))
                            {
                                var widget = parent.widgetDict[modId];
                                widget.Refresh();
                            }
                        }
                    }
                }

                _modModObj.SetActiveState(newState);
                RefreshBtnContent();
            }catch (Exception){}
        }
    }
}
